package hyl.base.medium;

import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_ProfileRGB;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferInt;
import java.awt.image.Raster;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.bytedeco.javacv.CanvasFrame.Exception;

/**
 * 关于gui 相关的函数
 * 
 * @author 37798955@qq.com
 *
 */
public class MyUI {
	/**
	 * 获取显示设备伽马值
	 * 
	 * @param screen
	 * @return
	 */
	public static double get伽马(int screenNumber) {
		GraphicsDevice[] screens = get多屏驱动();
		if (screenNumber >= 0 && screenNumber < screens.length) {
			return get伽马(screens[screenNumber]);
		} else {
			return 0.0;
		}
	}

	/**
	 * 获取默认显示设备伽马值 伽马就是显示器与图像的颜色偏差
	 * 
	 * @param screen
	 * @return
	 */
	public static double get当前屏伽马() {
		return get伽马(get当前屏驱动());
	}

	/**
	 * 获取显示设备伽马值
	 * 
	 * @param screen
	 * @return
	 */
	public static double get伽马(GraphicsDevice 屏幕驱动) {
		ColorSpace cs = 屏幕驱动.getDefaultConfiguration().getColorModel().getColorSpace();
		if (cs.isCS_sRGB()) {
			return 2.2;
		} else {
			try {
				return ((ICC_ProfileRGB) ((ICC_ColorSpace) cs).getProfile()).getGamma(0);
			} catch (RuntimeException e) {
			}
		}
		return 0.0;
	}

	public static GraphicsDevice get指定屏驱动(int 第几个屏) throws Exception {
		GraphicsDevice[] screens = get多屏驱动();
		if (第几个屏 >= screens.length) {
			throw new Exception("你指定的屏不存在");
		}
		return screens[第几个屏];// .getDefaultConfiguration();
	}

	public static GraphicsDevice[] get多屏驱动() {
		return GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
	}

	public static GraphicsDevice get当前屏驱动() {
		return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
	}

	public static void do委托并等待(Runnable ui事件) {
		if (EventQueue.isDispatchThread()) {
			ui事件.run();
		} else {
			try {
				// 等待事件执行完成,确保函数同步进行
				EventQueue.invokeAndWait(ui事件);
			} catch (java.lang.Exception ex) {
			}
		}
	}

	public static void do委托(Runnable ui事件) {
		if (EventQueue.isDispatchThread()) {
			ui事件.run();
		} else {
			try {
				// 等待事件执行完成,确保函数同步进行
				EventQueue.invokeLater(ui事件);
			} catch (java.lang.Exception ex) {
			}
		}
	}

	public static void showImage(BufferedImage image) {
		int width = image.getWidth(), height = image.getHeight();
		JLabel label = new JLabel();
		label.setSize(width, height);
		label.setIcon(new ImageIcon(image));

		JFrame frame = new JFrame();
		frame.setSize(width, height);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(label);
		frame.setVisible(true);
	}

	public static BufferedImage intBuffer2RGB(IntBuffer src, int width, int height) {
		return intBuffer2Image(src, width, height, BufferedImage.TYPE_INT_RGB);
	}

	public static BufferedImage byteBuffer2BGR(ByteBuffer src, int width, int height) {
		return byteBuffered2Image(src, width, height, BufferedImage.TYPE_3BYTE_BGR);
	}

	public static BufferedImage intBuffer2BGR(IntBuffer src, int width, int height) {
		return intBuffer2Image(src, width, height, BufferedImage.TYPE_INT_BGR);
	}

	/**
	 * 24位BGR转BufferedImage
	 * 
	 * @param src   -源数据
	 * @param width -宽度
	 * @param       height-高度
	 * @return
	 */
	public static BufferedImage byteBuffered2Image(ByteBuffer src, int width, int height, int 类型) {
		BufferedImage image = new BufferedImage(width, height, 类型);
		Raster ra = image.getRaster();
		DataBuffer out = ra.getDataBuffer();
		DataBufferByte db = (DataBufferByte) out;
//	       
//	        System.out.println( db.getData().length);
//	        System.out.println( src.position());
//	        System.out.println( src.capacity());
		// System.out.println( src.array().length);

		ByteBuffer.wrap(db.getData()).put(src);
		return image;
	}

	/**
	 * 24位整型BGR转BufferedImage
	 * 
	 * @param src   -源数据
	 * @param width -宽度
	 * @param       height-高度
	 * @return
	 */
	public static BufferedImage intBuffer2Image(IntBuffer src, int width, int height, int 类型) {
		BufferedImage image = new BufferedImage(width, height, 类型);
		Raster ra = image.getRaster();
		DataBuffer out = ra.getDataBuffer();
		DataBufferInt db = (DataBufferInt) out;
		IntBuffer.wrap(db.getData()).put(src);
		return image;
	}

}
